<template>
  <zero-vxe-grid v-loading="loading">
    <div slot="header">
      <!--    查询条件-->
      <el-collapse-transition>
        <div v-show="viewModuleOptions.showSearch">
          <vxe-form ref="queryForm" :data="viewModuleOptions.searchForm" title-align="right">
            <vxe-form-item title="名称" field="name" :item-render="{}">
              <template #default="{ data }">
                <vxe-input max-length="50" v-model="data.name" showWordCount clearable placeholder="请输入名称"/>
              </template>
            </vxe-form-item>

            <vxe-form-item title="编码" field="code" :item-render="{}">
              <template #default="{ data }">
                <vxe-input max-length="50" v-model="data.code" showWordCount clearable placeholder="请输入编码"/>
              </template>
            </vxe-form-item>
            <vxe-form-item title="创建时间" field="createTime" :item-render="{}">
              <template #default="{ data }">
                <el-date-picker v-model="data.createTime" style="width: 240px" value-format="yyyy-MM-dd"
                                type="daterange" range-separator="-" start-placeholder="开始日期"
                                end-placeholder="结束日期"
                                :picker-options="viewModuleOptions.pickerOptions" :editable="false"/>
              </template>
            </vxe-form-item>
            <vxe-form-item>
              <vxe-button content="搜索" round @click="searchSubmit" status="primary"/>
              <vxe-button content="重置" round @click="searchResetForm"/>
            </vxe-form-item>
          </vxe-form>
        </div>
      </el-collapse-transition>
    </div>
    <div slot="main">
      <!--表格-->
      <vxe-grid v-bind="viewModuleOptions" :ref="viewModuleOptions.ref" v-on="viewModuleOptions.fns">
        <template #buttons>
          <vxe-button content="新增" status="primary" icon="vxe-icon-add"
                      @click="initDialog({ref:addUpdateDialogRef,fun:handleAddFormData})"
                      v-hasPermi="['sysConfig:add']"/>
          <vxe-button content="批量删除" status="danger" icon="vxe-icon-delete" @click="handleDelete"
                      v-hasPermi="['sysConfig:delete']" :disabled="viewModuleOptions.dataListSelections.length <= 0"/>
          <vxe-button content="导出" status="success" icon="vxe-icon-cloud-download"
                      @click="handleExport({ref:importDialogRef,title:'参数设置'})"
                      v-hasPermi="['sysConfig:export']"/>
          <vxe-button content="导入" icon="vxe-icon-cloud-upload" v-hasPermi="['sysConfig:import']"
                      @click="initDialog({ref:importDialogRef})"/>
        </template>

        <template #tools>
          <vxe-button :title="viewModuleOptions.showSearch ? '隐藏搜索' : '显示搜索'" @click="showOrHideClick"
                      icon="vxe-icon-search" circle/>
          <vxe-button :title="viewModuleOptions.isFullscreen ? '还原' : '全屏'" @click="toggleIsFullscreen"
                      class="tool-btn"
                      :icon="viewModuleOptions.isFullscreen ? 'vxe-icon-minimize' : 'vxe-icon-fullscreen'" circle/>
        </template>

        <template #operation="{ row }">
          <vxe-button mode="text" status="primary" content="修改" v-hasPermi="['sysConfig:update']"
                      @click="initDialog({ref:addUpdateDialogRef,row:row,fun:handleUpdateFormData})"></vxe-button>
          <vxe-button mode="text" status="danger" content="删除" v-hasPermi="['sysConfig:delete']"
                      @click="handleDelete(row)"></vxe-button>
        </template>
      </vxe-grid>

      <!-- 添加、修改 -->
      <add-update :ref="addUpdateDialogRef"
                  :getDataList="getDataList"/>
      <!-- 导入、导出 -->
      <ZeroImportExport :ref="importDialogRef"
                        :searchForm="viewModuleOptions.searchForm"
                        :importUrl="viewModuleOptions.importUrl"
                        :importTemplateUrl="viewModuleOptions.importTemplateUrl"
                        :exportUrl="viewModuleOptions.exportUrl"
                        :getDataList="getDataList"/>
    </div>
  </zero-vxe-grid>
</template>

<script>

import viewModule from '@/mixins/vxe-grid.js'; // 表格js
import {checkPermi} from "@/utils/permission.js"; //权限js
import addUpdate from './components/add-update.vue' // 添加、修改模块


export default {
  name: "sysConfig",
  mixins: [viewModule],
  components: {
    addUpdate
  },
  data() {
    return {
      addUpdateDialogRef: 'addUpdateDialogRef',
      importDialogRef: 'importDialogRef',
      viewModuleOptions: {
        dataUrl: '/sysConfig/pageList',
        removeUrl: '/sysConfig',
        exportUrl: '/sysConfig/export',
        importUrl: '/sysConfig/import',
        importTemplateUrl: '/sysConfig/importTemplate',
        columns: [
          {type: 'checkbox', width: 60},
          {field: 'name', title: '名称', showOverflow: true, resizable: true},
          {field: 'code', title: '编码', resizable: true},
          {field: 'value', title: '值', resizable: true},
          {field: 'remarks', title: '备注', showOverflow: true, resizable: true},
          {field: 'createTime', title: '创建时间', sortable: true, width: '150px'},
          {field: 'updateTime', title: '修改时间', width: '150px'},
          {
            title: '操作', width: 150, slots: {default: 'operation'},
            visible: checkPermi(['sysConfig:update', 'sysConfig:delete', 'sysConfig:details'])
          }
        ]
      }
    };
  },
  watch: {
    //时间筛选特殊处理，重置createTime时也将根据这个字段衍生出来的字段置为null
    'viewModuleOptions.searchForm.createTime': function (val) {
      this.viewModuleOptions.searchForm.startCreateTime = val ? val[0] : null
      this.viewModuleOptions.searchForm.endCreateTime = val ? val[1] : null
    }
  },
  methods: {
    //处理表单数据
    handleUpdateFormData(formInfo) {
      if (formInfo.picJson) {
        formInfo.picList = formInfo.picJson.map(function (item) {
          return {url: item, response: {data: item}};
        })
        this.$refs[this.addUpdateDialogRef].uploadImage.listLengthTag = formInfo.picList.length
      } else {
        this.$refs[this.addUpdateDialogRef].uploadImage.listLengthTag = 0
      }
      if (formInfo.pic) {
        formInfo.pic1 = [{url: formInfo.pic, response: {data: formInfo.pic}}]
        this.$refs[this.addUpdateDialogRef].uploadImage2.listLengthTag = formInfo.picList.length
      } else {
        this.$refs[this.addUpdateDialogRef].uploadImage2.listLengthTag = 0
      }
    },
    //处理表单数据
    handleAddFormData(formInfo) {
      this.$refs[this.addUpdateDialogRef].uploadImage.listLengthTag = 0
      this.$refs[this.addUpdateDialogRef].uploadImage2.listLengthTag = 0
    },
  }
}
</script>
